/** @Date 下午5:10:47
  * @version 1.0.0
  * @author shangyd
  * Copyright (c) 2015
  */
package com.sdw.soft.activemq;

import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class JMSMessageSender {

	public static final String JMS_MESSAGE_TYPE_OBJECT = "JMS_MESSAGE_OBJECT";
	private static Map<String,Queue> queues = null;
	private static JMSMessageSender jmsSender= null;
	private static String brokerUrl = "tcp://192.168.183.133:61616";

	private JMSMessageSender(){
		queues = new ConcurrentHashMap<String, Queue>();
	}
	
	public Map<String, Queue> getQueues() {
		return queues;
	}

	private synchronized static void initJMSSender(){
		if(jmsSender == null){
			jmsSender = new JMSMessageSender();
			Thread runner = new Thread(new JMSMessageRunner(brokerUrl));
			runner.start();
		}
	}
	public static JMSMessageSender instance(){
		if(jmsSender == null){
			initJMSSender();
		}
		return jmsSender;
	}
	public synchronized Map<String,Queue> copyAndClearMap(){
		Map<String,Queue> map = new ConcurrentHashMap<String, Queue>(queues);
		queues.clear();
		return map;
	}
	public synchronized boolean addMessage(String queueName,Map content){
		if(content == null){
			return false;
		}
		if(queues.containsKey(queueName)){
			queues.get(queueName).add(content);
		}else{
			Queue<Map> queue = new ConcurrentLinkedQueue<Map>();
			queue.add(content);
			queues.put(queueName, queue);
		}
		return true;
	}
}
